package cn.cdeden.knowledge.controller.online.search;

import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.domain.PersonalFilesTree;
import cn.cdeden.knowledge.domain.dto.FilesTree;
import cn.cdeden.knowledge.domain.dto.PortalQueryParam;
import cn.cdeden.knowledge.domain.dto.SearchFilesInfoDto;
import cn.cdeden.knowledge.domain.vo.FileAnalysisDataVo;
import cn.cdeden.knowledge.enums.TreeTypeEnum;
import cn.cdeden.knowledge.service.PersonalFilesTreeService;
import cn.cdeden.knowledge.utils.DateUtil;
import cn.cdeden.knowledge.utils.PageUtil;
import cn.cdeden.system.api.model.LoginUser;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
@Component("SearchFiles_personal")
public class SearchFilesInfoPersonal extends SearchFilesInfo{

    private final PersonalFilesTreeService service;

    @Override
    public TableDataInfo<FilesTree> searchInfo(SearchFilesInfoDto infoDto, Pageable pageable) {
        log.info("执行");
        TableDataInfo<FilesTree> filesTreePageInfo = service.selectPersonalTreePage(infoDto, pageable);
        return filesTreePageInfo;
    }

    @Override
    public TableDataInfo<FilesTree> queryNodeFileInfoPage(PortalQueryParam query, LoginUser userDto, Pageable pageable) {
        IPage queryPage = PageUtil.toMybatisPage(pageable);
        LambdaQueryChainWrapper<PersonalFilesTree> wrapper = getWrapper(query, userDto);
        if (ObjectUtils.isEmpty(wrapper)){
            return new TableDataInfo<>();
//            return ConvertUtil.convertPage(new PageInfo<>(0, new ArrayList<>()), FilesTree.class, pageable);
        }

        IPage<PersonalFilesTree> page = wrapper.page(queryPage);
//        TableDataInfo.build(page);
        return MapstructUtils.convertTableDataInfo(TableDataInfo.build(page), FilesTree.class);
//        return ConvertUtil.convertPage(page, FilesTree.class, pageable);
    }

    @Override
    public List<FilesTree> queryNodeFileInfo(PortalQueryParam query, LoginUser userDto) {
        LambdaQueryChainWrapper<PersonalFilesTree> wrapper = getWrapper(query, userDto);
        if (ObjectUtils.isEmpty(wrapper)){
            return new ArrayList<FilesTree>();
//            return ConvertUtil.convertList(new ArrayList<>(), FilesTree.class);
        }
        List<PersonalFilesTree> list = wrapper.list();
        return MapstructUtils.convert(list, FilesTree.class);
//        return ConvertUtil.convertList(list, FilesTree.class);
    }

    @Override
    public List<FileAnalysisDataVo> getNewFileAnalysisData(Map<String, List<String>> resultMap) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        List<FileAnalysisDataVo> fileAnalysisDataVos = new ArrayList<>();
        resultMap.forEach( (k,v) -> {
            List<PersonalFilesTree> list = service.list(new LambdaQueryWrapper<PersonalFilesTree>()
                .eq(PersonalFilesTree::getCreateId, loginUser.getUserId().toString())
                .in(PersonalFilesTree::getSuffix, v));
            FileAnalysisDataVo fileAnalysisDataVo = new FileAnalysisDataVo();
            fileAnalysisDataVo.setFileSize(list.size());
            fileAnalysisDataVo.setTypeName(k);
            fileAnalysisDataVos.add(fileAnalysisDataVo);
        });
        return fileAnalysisDataVos;
    }


    private  LambdaQueryChainWrapper<PersonalFilesTree> getWrapper(PortalQueryParam query, LoginUser userDto) {
        LambdaQueryChainWrapper<PersonalFilesTree> wrapper = service.lambdaQuery();
        List<String> nodeIdList = query.getNodeId(); //是否查询某个节点
        if (CollectionUtils.isNotEmpty(nodeIdList)) {
            //需要验证是否是这个用户的文件夹
            service.selectByNodeIdIsOwn(nodeIdList, String.valueOf(userDto.getUserId()), false);

//            查询节点下的全部文件夹信息
            List<PersonalFilesTree> filesTrees = service.selectJuniorAll(nodeIdList, "1");
            if (CollectionUtils.isEmpty(filesTrees)) {
                return null;
            }
            //查询节点下的全部文件信息
            List<Long> longList = filesTrees.stream().filter(tree -> StringUtils.equals(TreeTypeEnum.FILE.getType(), tree.getType())).map(PersonalFilesTree::getId).collect(Collectors.toList());

            if (CollectionUtils.isEmpty(longList)){
                return null;
            }

            wrapper.in(PersonalFilesTree::getId, longList);
        } else {
            wrapper.eq(PersonalFilesTree::getCreateId, userDto.getUserId())
                .eq(PersonalFilesTree::getType, TreeTypeEnum.FILE.getType())
                .isNull(PersonalFilesTree::getDeleteId);
        }

        //时间的判断
        if (ObjectUtil.isNotEmpty(query.getStartDate())) {
            wrapper.between(PersonalFilesTree::getCreateTime, DateUtil.toDay(query.getStartDate()), DateUtil.setEndHMS(query.getEndDate()));
        }
        //排序
        wrapper.orderByAsc(PersonalFilesTree::getParentId,PersonalFilesTree::getCreateTime);


        return wrapper;
    }






}
